/*
  ID: nemokin1
  PROG: prefix
  LANG: C++
*/
// this is a good problem for me to train my programming skills
// using tries, debug for 3hours because of not-correctly-init.--!
// firstly i use memorised dfs, but it seemed to much deep in
// recursive and then i use dp...And finally it passed with
// submission#9.

#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <fstream>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <fstream>
 
using namespace std;
 
#define REP(i,n) for(int i=0; i<(n); ++i)
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define FORD(i,a,b) for(int i=(a);i>=b;--i)
#define FOREACH(it,c) for(typeof((c).begin()) it=(c).begin();it!=(c).end();++it)
#define ALL(c) (c).begin(),(c).end()
 
#define debug(x) cerr<< #x << " = " << (x) << "\n";
#define debugv(v) cerr << #v << " = "; FOREACH(it,v) cerr << *it <<","; cerr << "\n";
#define SIZE(a)  (int(a.size()))
 
template<typename T> inline void checkMax(T& a, T b) {if (b>a) a=b;}
template<typename T> inline void checkMin(T& a, T b) {if (b<a) a=b;}
 
typedef long long LL;
typedef vector<int> VI;
typedef vector<string> VS;
typedef long double ld;
const int INF = 1000000000;

typedef struct Node
{
    Node(){val=""; for(int i = 0; i< 29; i++) a[i]=NULL;}
    string val;
    struct Node* a[29];
}Node;

int maxlen = 0;
Node *head;

int check(Node *head, string val)
{
    Node *next = head;
    for(int i = 0; i < int(val.size()); i++){
	if(next->a[val[i]-'A'] == NULL) return false;
	next = next->a[val[i]-'A'];
    }
    if(next->val == val) return true;
    return -2;
}
int dp[1000000];
string pref;

// while...dfs seems to many recursives for the OS-recursive-limit
/*
int dfs(int start)
{
    int &no = dp[start];
    if(no != -1) return dp[no];
    
    string s = pref.substr(start);
    for(int i = 1; i <= int(s.size()); i++){
	int ot = check(head,s.substr(0,i));
	if(ot == true){
	    checkMax(no,i+dfs(start+i));
	}else if(ot == false){
	    break;
	}
    }
    return no==-1?0:no;
}
*/

int main() {
    //freopen("prefix.in","r",stdin);freopen("prefix.out","w",stdout);
    ifstream input("prefix.in");
    ofstream output("prefix.out");
    vector<string> oo;
    
    string temp;
    while(input >> temp){
	if(temp == ".")
	    break;
	oo.push_back(temp);
    }
    
    while(input >> temp){
     	pref+=temp;
    }

    // build tries
    head = new Node();
    head->val = "";
    
    for(int i = 0; i < int(oo.size()); i++){
    	string s = oo[i];
    	Node *next = head;
    	for(int lv = 0; lv < int(s.size()); lv++){
    	    if(next->a[s[lv]-'A'] == NULL){
    		Node *node = new Node();
    		next->a[s[lv]-'A'] = node;
    		next = node;
    	    }
    	    else{
    		next = next->a[s[lv]-'A'];
    	    }
    	}
    	next->val = s;
    }

    int n = int(pref.size());
    memset(dp,0,sizeof(dp));
    for(int i = n-1; i>=0; i--){
	for(int k = 1; k <= n-i; k++){
	    int state = check(head,pref.substr(i,k));
	    if(state == true) checkMax(dp[i],k+dp[i+k]);
	    if(state == false) break;
	}
    }
    
    output << dp[0] << endl;
    
    exit(0);
}
